package com.example.shangjinlieren.fractionPage;

import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.compontents.ImageNetWork;
import com.example.shangjinlieren.entity.myCenter.Grid;
import com.example.shangjinlieren.model.myCenter.MyCenterGridsModel;
import com.example.shangjinlieren.tabbar.fraction.BaseFraction;
import com.example.shangjinlieren.theme.MyColors;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import java.util.List;

public class MyCenter extends BaseFraction {
    private Context context;
    private AbilitySlice abilitySlice;
    private DirectionalLayout component;
    private LayoutScatter scatter;
    private DirectionalLayout  mybody;
    private ComponentContainer container;
    private Intent intent;
    public MyCenter(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        //获取fraction 体
        this.scatter=scatter;
        this.container = container;
        this.intent =intent;

        this.context = container.getContext();
        this.component =(DirectionalLayout) scatter.parse(ResourceTable.Layout_my_center, container, false);
        return component;
    }
    private void initPage(){
          mybody =(DirectionalLayout) component.findComponentById(ResourceTable.Id_mybody);
        inittopCard();
        initMenus();
    }
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initPage();

    }
    protected void login(){

    }
    protected void inittopCard(){
        StackLayout my_center_userinfo_card =(StackLayout)  scatter.parse(ResourceTable.Layout_my_center_userinfo_card, container, false);
        Image useridimg =(Image) my_center_userinfo_card.findComponentById(ResourceTable.Id_useridimg);
        Text username =(Text) my_center_userinfo_card.findComponentById(ResourceTable.Id_username);
        useridimg.setWidth((int) RpxUtil.rpx(110,context));
        useridimg.setHeight((int) RpxUtil.rpx(110,context));
        useridimg.setCornerRadius((int) RpxUtil.rpx(110,context));
        Image my_center_userinfo_card_bgimg =(Image) my_center_userinfo_card.findComponentById(ResourceTable.Id_my_center_userinfo_card_bgimg);
        my_center_userinfo_card_bgimg.setScaleMode(Image.ScaleMode.CLIP_CENTER);
        //背景图片 https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2876171725,2307533980&fm=26&gp=0.jpg
        new ImageNetWork(this.abilitySlice,my_center_userinfo_card_bgimg,
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201609%2F16%2F20160916214141_rJYX4.png&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618110228&t=cdef6781a76d1d2f742956e94a491a19")
                .start();

        DependentLayout  my_center_userinfo_card_DependentLayout =(DependentLayout) my_center_userinfo_card.findComponentById(ResourceTable.Id_my_center_userinfo_card_DependentLayout);
        //设置边距
        my_center_userinfo_card_DependentLayout.setPaddingLeft((int) RpxUtil.rpx(30,context));
        my_center_userinfo_card_DependentLayout.setPaddingTop((int) RpxUtil.rpx(30,context));
        my_center_userinfo_card_DependentLayout.setPaddingRight((int) RpxUtil.rpx(30,context));
        my_center_userinfo_card_DependentLayout.setPaddingBottom((int) RpxUtil.rpx(30,context));
        //设置背景颜色
        ShapeElement my_center_userinfo_card_DependentLayoutElement = new ShapeElement();

        my_center_userinfo_card_DependentLayoutElement.setRgbColor(MyColors.mask);
        my_center_userinfo_card_DependentLayout.setBackground(my_center_userinfo_card_DependentLayoutElement);
//        my_center_userinfo_card.setMarginTop((int) RpxUtil.rpx(30,context));
        my_center_userinfo_card.setWidth((int) RpxUtil.rpx(750,context));
        my_center_userinfo_card.setHeight((int) RpxUtil.rpx(280,context));
        ShapeElement  my_center_userinfo_cardElement  = new ShapeElement();
        my_center_userinfo_card.setBackground(my_center_userinfo_cardElement);
        my_center_userinfo_card.setAccessibility(1);
        //设置头像图片
        //        inside
        useridimg.setScaleMode(Image.ScaleMode.INSIDE);
        new ImageNetWork(this.abilitySlice,useridimg,"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201509%2F17%2F20150917190203_wtvs5.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618036490&t=15a2503d1a3fdaf9fbe431f101535a33").start();
        username.setTextSize((int) RpxUtil.rpx(36,context));
        username.setText("点击登录");
        username.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Operation operationBuilder = new Intent.OperationBuilder()
                        .withBundleName("com.example.shangjinlieren")
                        .withAbilityName("com.example.shangjinlieren.LoginAbility")
                        .build();
                intent.setOperation(operationBuilder);
                abilitySlice.startAbility(intent);
            }
        });
        username.setTextColor(Color.WHITE);
        username.setPaddingLeft((int) RpxUtil.rpx(20,context));

//        底部盒子
        DirectionalLayout my_center_userinfo_card_bottom_card =(DirectionalLayout) my_center_userinfo_card.findComponentById(ResourceTable.Id_my_center_userinfo_card_bottom_card);
        my_center_userinfo_card_bottom_card.setPaddingLeft((int) RpxUtil.rpx(20,context));
        my_center_userinfo_card_bottom_card.setPaddingTop((int) RpxUtil.rpx(20,context));
        my_center_userinfo_card_bottom_card.setPaddingBottom((int) RpxUtil.rpx(20,context));
        Text my_center_userinfo_card_current_canyv_nums =(Text) my_center_userinfo_card.findComponentById(ResourceTable.Id_my_center_userinfo_card_current_canyv_nums);
        my_center_userinfo_card_current_canyv_nums.setText("参与 0");
        Text my_center_userinfo_card_current_shouru_nums =(Text) my_center_userinfo_card.findComponentById(ResourceTable.Id_my_center_userinfo_card_current_shouru_nums);
        my_center_userinfo_card_current_shouru_nums.setPaddingLeft((int) RpxUtil.rpx(20,context));
        my_center_userinfo_card_current_shouru_nums.setText("收入 0");
        my_center_userinfo_card_current_canyv_nums.setTextSize((int) RpxUtil.rpx(28,context));
        my_center_userinfo_card_current_shouru_nums.setTextSize((int) RpxUtil.rpx(28,context));
        mybody.addComponent(my_center_userinfo_card);
    }
    protected void initMenus(){
        MyCenterGridsModel myCenterGridsModel = new MyCenterGridsModel();
        DirectionalLayout  my_center_gird =(DirectionalLayout)  scatter.parse(ResourceTable.Layout_my_center_gird, null, false);
        TableLayout Id_my_center_gird =(TableLayout) my_center_gird.findComponentById(ResourceTable.Id_my_center_gird);
        Id_my_center_gird.setWidth((int) RpxUtil.rpx(632,context));
        Id_my_center_gird.setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        List<Grid> grids = myCenterGridsModel.getGrids();
        for (int i=0;i<grids.size();i++){
            DirectionalLayout menuitem = new DirectionalLayout(context);
            Image icon = new Image(context);
            icon.setWidth((int) RpxUtil.rpx(80,context));
            icon.setHeight((int) RpxUtil.rpx(80,context));
            icon.setPixelMap(grids.get(i).getIcon());
            icon.setScaleMode(Image.ScaleMode.INSIDE);
            Text text = new Text(context);
            text.setText(grids.get(i).getName());
            text.setTextSize((int) RpxUtil.rpx(36,context));
            menuitem.setWidth((int) RpxUtil.rpx(260,context));
            menuitem.setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            text.setTextAlignment(TextAlignment.CENTER);
            text.setTextColor(new Color(MyColors.firstColor));
            text.setMarginTop((int) RpxUtil.rpx(20,context));
            menuitem.setAlignment(LayoutAlignment.START);
            ShapeElement menuitemElement = new ShapeElement();
            menuitemElement.setStroke(2,MyColors.secondBorderRgb);
            menuitemElement.setCornerRadius((int) RpxUtil.rpx(12,context));
            menuitemElement.setRgbColor(new RgbColor(255,255,255));
            menuitem.setBackground(menuitemElement);
            menuitem.addComponent(icon);
            menuitem.addComponent(text);
            menuitem.setMarginLeft((int) RpxUtil.rpx(30,context));
            menuitem.setMarginTop((int) RpxUtil.rpx(30,context));
//            内边距
            menuitem.setPaddingLeft((int) RpxUtil.rpx(20,context));
            menuitem.setPaddingTop((int) RpxUtil.rpx(20,context));
            menuitem.setPaddingRight((int) RpxUtil.rpx(20,context));
            menuitem.setPaddingBottom((int) RpxUtil.rpx(20,context));
            Id_my_center_gird.addComponent(menuitem);
        }
        Id_my_center_gird.setMarginLeft((int) RpxUtil.rpx(30,context));
        Id_my_center_gird.setMarginTop((int) RpxUtil.rpx(30,context));
        Id_my_center_gird.setMarginRight((int) RpxUtil.rpx(30,context));
        Id_my_center_gird.setMarginBottom((int) RpxUtil.rpx(30,context));
        Id_my_center_gird.setAlignmentType(TableLayout.DEFAULT);
        mybody.addComponent(my_center_gird);
    }
    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public int getUIContent() {
        return ResourceTable.Layout_my_center;
    }

    @Override
    public void initComponent() {

    }
}
